// src/pages/catering/shopCommendList/commendList.js
import getIn from '../../../../yali-sdk/utils/getIn';
import { SUCCESS } from '../../../../yali-sdk/utils/success';
import { FAIL } from '../../../../yali-sdk/utils/fail';
import { notify } from '../../../../yali-sdk/utils/NotifyRegistryCenter';
import shopCommendList from '../../../../yali-sdk/business/catering/shopCommendList';
import { updateVariableParams, showFail, showSuccess, uploadImage, getImagePath } from '../../../config/common';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    screenList: [{ name: '全部' }, { name: '好评', feedback: [4,5] }, { name: '中评', feedback: [2,3.5] }, { name: '差评', feedback: [0,1.5] }],
    currentIndex: 0,
    commendList: [],
    params: {
      page: 1,
      rows: 5,
    },
    loading: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: options.color,
    });
    this.setData({ color: options.color });
    this.fetchData();
  },
  fetchData() {
    const token = getIn(getApp(), ['globalData', 'token']);
    const shop_id = getIn(this.options, ['shop_id']);
    const params = { ...this.data.params, token, shop_id };
    wx.showLoading();
    this.setData({ loading: true });
    global.ActionDispatcher.execute(shopCommendList, params, (code, res) => {
      if(code == SUCCESS) {
        wx.stopPullDownRefresh();
        wx.hideLoading()
        let commendList = res.data.list;
        commendList = params.page == 1 ? commendList : this.data.commendList.concat(commendList);
        this.setData({ commendList, loading: false, total: res.data.total });
      } else if(code == FAIL) {
        wx.stopPullDownRefresh();
        wx.hideLoading();
        this.setData({ loading: false });
      }
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    const page = 1;
    const params = updateVariableParams(this.data.params, { page });
    this.data.params = params;
    this.fetchData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    const { total, commendList, loading } = this.data;
    if (commendList.length < total && !loading) {
      // 界面加载完, 且还有下一页的情况。
      const pages = getIn(this.data, ['params', 'page']) || 1; 
      const params = updateVariableParams(this.data.params, { page: pages + 1 });
      this.data.params = params;
      this.fetchData();
    }
  },
  // 筛选条件
  onSelect(e) {
    const currentIndex = getIn(e.currentTarget, ['dataset', 'currentindex']);
	  const feedback = currentIndex != 0 ? this.data.screenList[currentIndex].feedback:null;
    const params = updateVariableParams(this.data.params, { page: 1, feedback });
    this.data.params = params;
    this.fetchData();
    this.setData({ currentIndex });
  },
  // 预览图片
  previewImg(e) {
    //获取当前图片的下标
    const item = getIn(e.currentTarget, ['dataset', 'item']);
    const index = getIn(e.currentTarget, ['dataset', 'index']);
    wx.previewImage({
      //当前显示图片
      current: item[index],
      //所有图片
      urls: item
    })
  },
})